<template>
  <div class="app-container">
      <div class="fly-card-head">
         <div class="fly-card-head-wrapper">
             <div class="fly-card-head-title"><div class="head-title">小组话题列表</div></div>
         </div>
      </div>
      <div class="fly-card-body">
         <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="分类ID" prop="columnId">
        <el-input
          v-model="queryParams.columnId"
          placeholder="请输入分类ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小组ID" prop="groupId">
        <el-input
          v-model="queryParams.groupId"
          placeholder="请输入小组ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帖子标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入帖子标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否置顶" prop="istop">
        <el-input
          v-model="queryParams.istop"
          placeholder="请输入是否置顶"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否关闭" prop="isclose">
        <el-input
          v-model="queryParams.isclose"
          placeholder="请输入是否关闭"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否允许评论" prop="iscomment">
        <el-input
          v-model="queryParams.iscomment"
          placeholder="请输入是否允许评论"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否精华帖子" prop="isposts">
        <el-input
          v-model="queryParams.isposts"
          placeholder="请输入是否精华帖子"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="字典状态" clearable size="small" style="width: 240px">
            <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue+1" />
          </el-select>
        </el-form-item>

      <el-form-item label="推荐" prop="isrecommend">
        <el-input
          v-model="queryParams.isrecommend"
          placeholder="请输入推荐"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['group:topic:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['group:topic:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['group:topic:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            size="mini"
            @click="handleAllIndex"
            plain>重新索引</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="topicList" @selection-change="handleSelectionChange" style="width: 100%">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="帖子标题" align="left" prop="title" width="280" />
        <el-table-column label="小组名称" align="center" prop="groupName" />
        <el-table-column label="分类名称" align="center" prop="columnName" />
        <el-table-column label="用户作者" align="center" prop="nickname" />
        <el-table-column label="回复统计" align="center" prop="countComment" />
        <el-table-column label="浏览数" align="center" prop="countView" />
        <el-table-column label="喜欢数" align="center" prop="countLove" />
        <el-table-column label="精华" align="center" prop="isposts" :formatter="postsFormat" />
        <el-table-column label="关闭帖子" align="center" prop="isclose" :formatter="closeFormat" />
        <el-table-column label="允许评论" align="center" prop="iscomment" :formatter="commentFormat" width="180" />
        <el-table-column label="审核" align="center" prop="status" :formatter="statusFormat" />
        <el-table-column label="发布时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="160" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['group:topic:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['group:topic:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改小组话题对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="分类ID" prop="columnId">
            <el-input v-model="form.columnId" placeholder="请输入分类ID" />
          </el-form-item>
          <el-form-item label="小组ID" prop="groupId">
            <el-input v-model="form.groupId" placeholder="请输入小组ID" />
          </el-form-item>
          <el-form-item label="帖子标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入帖子标题" />
          </el-form-item>
          <el-form-item label="标签" prop="label">
            <el-input v-model="form.label" placeholder="内容相关标签" />
          </el-form-item>
          <el-form-item label="是否置顶" prop="istop">
            <el-radio-group v-model="form.istop">
                <el-radio v-for="dict in topOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          <el-form-item label="是否关闭" prop="isclose">
            <el-radio-group v-model="form.isclose">
                <el-radio v-for="dict in closeOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          <el-form-item label="评论开关" prop="iscomment">
            <el-radio-group v-model="form.iscomment">
              <el-radio v-for="dict in commentOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="评论显示" prop="iscommentshow">
            <el-radio-group v-model="form.iscommentshow">
              <el-radio v-for="dict in commentshowOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="精华帖子" prop="isposts">
            <el-radio-group v-model="form.isposts">
              <el-radio v-for="dict in postsOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="推荐" prop="recommends">
            <el-select v-model="form.recommends" multiple placeholder="请选择">
              <el-option v-for="item in recommendOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listTopic, getTopic, delTopic, addTopic, updateTopic, exportTopic } from "@/api/group/topic";
import { indexTopic } from "@/api/elastic/indexTopic";
export default {
  name: "Topic",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 置顶状态数据字典
      topOptions: [],
      // 精华帖子状态数据字典
      postsOptions: [],
      // 关闭帖子状态数据字典
      closeOptions: [],
      // 关闭评论状态数据字典
      commentOptions: [],
      // 评论内容是否显示状态数据字典
      commentshowOptions: [],
      // 内容推荐设置态数据字典
      recommendOptions: [],
      // 审核状态数据字典
      statusOptions: [],
      // 小组话题表格数据
      topicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        columnId: undefined,
        groupId: undefined,
        userId: undefined,
        title: undefined,
        label: undefined,
        content: undefined,
        countComment: undefined,
        countView: undefined,
        countLove: undefined,
        istop: undefined,
        isclose: undefined,
        iscomment: undefined,
        iscommentshow: undefined,
        isposts: undefined,
        isaudit: undefined,
        isdelete: undefined,
        recommends: [],
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "帖子标题不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('content_top').then(response => {
      this.topOptions = response.data;
    });
    this.getDicts('content_posts').then(response => {
      this.postsOptions = response.data;
    });
    this.getDicts('content_close').then(response => {
      this.closeOptions = response.data;
    });
    this.getDicts('comment_switch').then(response => {
      this.commentOptions = response.data;
    });
    this.getDicts('comment_show').then(response => {
      this.commentshowOptions = response.data;
    });
    this.getDicts('content_recommend').then(response => {
      this.recommendOptions = response.data;
    });
    this.getDicts('content_verify').then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询小组话题列表 */
    getList() {
      this.loading = true;
      listTopic(this.queryParams).then(response => {
        this.topicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 岗位状态字典翻译
    postsFormat(row, column) {
      return this.selectDictLabel(this.postsOptions, row.isposts);
    },
    // 帖子是否关闭状态字典翻译
    closeFormat(row, column) {
      return this.selectDictLabel(this.closeOptions, row.isclose);
    },
    // 评论状态状态字典翻译
    commentFormat(row, column) {
      return this.selectDictLabel(this.commentOptions, row.iscomment);
    },
    // 评论状态状态字典翻译
    commentshowFormat(row, column) {
      return this.selectDictLabel(this.commentshowOptions, row.iscommentshow);
    },
    // 内容推荐设置态数据字典
    recommendFormat(row, column) {
      return this.selectDictLabel(this.recommendOptions, row.iscommentshow);
    },
    // 审核状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        columnId: undefined,
        groupId: undefined,
        userId: undefined,
        title: undefined,
        label: undefined,
        content: undefined,
        countComment: undefined,
        countView: undefined,
        countLove: undefined,
        istop: 0,
        isclose: 0,
        iscomment: 0,
        iscommentshow: 0,
        isposts: 0,
        isaudit: 0,
        recommend: 0,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小组话题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTopic(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改话题属性";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTopic(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addTopic(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除小组话题编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTopic(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有小组话题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTopic(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
     /** 全部索引按钮操作 */
    handleAllIndex() {
      const h = this.$createElement;
      var msg;
      this.$msgbox({
        title: '索引所有话题',
        message: h('p', null, [
          h('span', null, '索引所有话题内容')
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '执行中...';
            indexTopic().then(response => {
              if (response.code === 200) {
                msg = response.msg
                done();
                instance.confirmButtonLoading = false;
              } else {
                done();
                this.msgError(response.msg);
              }
            });
          } else {
            done();
          }
        }
      }).then(() => {
        this.$message({
          type: 'success',
          message: msg
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: msg
        });
      });;
    }
  }
};
</script>
<style>
  .el-table th {
    color: #515a6e;
    background-color: #f8f8f9;
    font-size: 15px;
  }
  .el-table .el-table__header-wrapper th {
      font-size: 15px;
      color: #515a6e;
  }
</style>
